<template>
	<el-container>
		<el-aside width="240px">
			<el-container>
				<el-main class="nopadding">
					<blCategoryTree title="促销分类" ref="group" model="Promotion" @node-click="groupClick" />
				</el-main>
			</el-container>
		</el-aside>
		<el-container>
			<el-header>
				<div class="left-panel">
					<el-button type="primary" icon="el-icon-plus" @click="add">创建卡券</el-button>
					<div style="width: 15px;">&nbsp;</div>
					<el-date-picker v-model="search.dateRange" type="daterange" align="right" unlink-panels
						range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="YYYY-MM-DD">
					</el-date-picker>
					<div class="right-panel-search" style="padding-left: 15px;">
						<el-input v-model="search.keyword" placeholder="计划名称" clearable></el-input>
						<el-button type="primary" icon="el-icon-search" @click="upsearch">查询</el-button>
					</div>
				</div>
				<div class="right-panel">
					<div class="right-panel-search">

					</div>
				</div>
			</el-header>
			<div>
				<el-card shadow="never">
					<sc-select-filter :data="state" :selected-values="selectedValues" :label-width="80"
						@on-change="stateChange"></sc-select-filter>
				</el-card>
			</div>
			<el-main class="nopadding">
				<scTable ref="table" :apiObj="apiObj.list" @selection-change="selectionChange" stripe remoteSort
					remoteFilter>
					<el-table-column type="selection" width="50"></el-table-column>
					<el-table-column label="ID" prop="id" width="150">
						<template #default="scope">
							<span @click="table_edit(scope.row, scope.$index)"> {{scope.row.id}} </span>
						</template>
					</el-table-column>
					<el-table-column v-if="false" align="center" width="90" label="活动封面" column-key="filterAvatar"
						:filters="[{text: '已上传', value: '1'}, {text: '未上传', value: '0'}]">
						<template #default="scope">
							<template v-if="scope.row.activity_image">
								<el-avatar :src="scope.row.activity_image + '?x-oss-process=image/resize,w_90'"
									size="large"></el-avatar>
							</template>
							<template v-else>
								<p style="text-align: center;">无图</p>
							</template>
						</template>
					</el-table-column>
					<el-table-column label="促销卡券名称" prop="promo_name" width="220"></el-table-column>
					<el-table-column label="券描述" prop="promo_description" width="250"></el-table-column>
					<el-table-column label="发行量" prop="promo_total_quantity" width="100"></el-table-column>
					<el-table-column label="推广模式" prop="promo_mode" width="100">
						<template #default="{row}">
							{{row.promo_mode=='system'?'系统发放':'用户领取'}}
						</template>
					</el-table-column>
					<el-table-column label="起止时间" prop="created_at" width="170" sortable='custom'>
						<template #default="scope">
							{{scope.row.promo_starts_at}} <br> {{scope.row.promo_ends_at}}
						</template>
					</el-table-column>
					<el-table-column label="促销活动备注" prop="promo_remark" width="220">
						<template #default="scope">
							{{scope.row.activity_remark}}
						</template>
					</el-table-column>
					<el-table-column label="操作" fixed="right" align="right" width="160">
						<template #default="scope">
							<el-button-group>
								<el-button text type="primary" size="small"
									@click="table_edit(scope.row, scope.$index)">编辑</el-button>
							</el-button-group>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>
</template>

<script>
	import saveDialog from './save'
	import scSelectFilter from '@/components/scSelectFilter'

	export default {
		name: 'promotion.manage',
		components: {
			saveDialog,
			scSelectFilter
		},
		data() {
			return {
				state: [{
					title: "计划状态",
					key: "计划状态",
					options: [{
							label: "全部",
							value: null
						},
						{
							label: "进行中",
							value: "进行中",
							icon: "el-icon-flag"
						},
						{
							label: "未开始",
							value: "未开始",
							icon: "el-icon-bottom-left"
						},
						{
							label: "已结束",
							value: "已结束",
							icon: "el-icon-bottom-left"
						}
					]
				}],
				selectedValues: {
					state: [""],
					type: [""]
				},
				dialog: {
					save: false
				},
				apiObj: this.$API.system.promotion,
				selection: [],
				search: {
					keyword: null,
					dateRange: null
				}
			}
		},
		methods: {
			stateChange(e) {
				var params = {
					state: e
				}
				this.$refs.table.upData(params)
			},
			//添加
			add() {
				this.dialog.save = true
				this.$nextTick(() => {
					const checkedNode = this.$refs.group.getCurrentNode();
					this.$refs.saveDialog.open().setData({
						category_id: checkedNode?.id || null
					});
				})
			},
			//编辑
			table_edit(row) {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row)
				})
			},
			//查看
			table_show(row) {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('show').setData(row)
				})
			},
			//删除
			async table_del(row, index) {
				var reqData = {
					id: row.id
				}
				var res = await apiObj.remove.post(reqData);
				if (res.code == 0) {
					this.$refs.table.tableData.splice(index, 1);
					this.$message.success(res.msg)
				} else {
					this.$alert(res.message, "提示", {
						type: 'error'
					})
				}
			},
			//批量删除
			async batch_del() {
				this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
					type: 'warning'
				}).then(() => {
					const loading = this.$loading();
					this.selection.forEach(item => {
						this.$refs.table.tableData.forEach((itemI, indexI) => {
							if (item.id === itemI.id) {
								this.$refs.table.tableData.splice(indexI, 1)
							}
						})
					})
					loading.close();
					this.$message.success("操作成功")
				}).catch(() => {

				})
			},
			//表格选择后回调事件
			selectionChange(selection) {
				this.selection = selection;
			},
			//树点击事件
			groupClick(data) {
				var params = {
					category_id: data.id
				}
				this.$refs.table.upData(params)
			},
			//搜索
			upsearch() {
				this.$refs.table.upData(this.search)
			},
			//本地更新数据
			handleSuccess(data, mode) {
				if (mode == 'add') {
					this.$refs.table.refresh()
				} else if (mode == 'edit') {
					this.$refs.table.refresh()
				}
			}
		}
	}
</script>

<style>
</style>
